<template>
  <div class="blogConBox">
    <blogInfo v-if="showType!==0" :blogObj="blogObj" @reload="reload"></blogInfo>
    <template v-else>
      <div class="blogCons">
        <transition-group name="list" tag="div">
          <div
            class="blogBox"
            v-for="item in currentIndex"
            :key="item.blogId"
            @click="showBlog(item)"
          >
            <div class="blogTit">{{item.title}}</div>
            <div class="blogContent">
              <span>分类：</span>
              <template v-if="item.tags">
                <el-tag v-for="tag in item.tags" :key="tag.tagId">{{tag.name}}</el-tag>
              </template>
            </div>
            <div class="blogTime">
              <span>作者：{{item.user.userName}}</span>
              <span>创建时间：{{item.createDate}}</span>
            </div>
          </div>
        </transition-group>

        <div class="marBox">
          <el-pagination
            @size-change="(val)=>{this.pageSize=val}"
            @current-change="(val)=>{this.pageIndex=val}"
            :current-page="pageIndex"
            :page-sizes="[5, 10, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          ></el-pagination>
        </div>
      </div>
      <div class="blogFilter">
        <div class="blogSticky">
          <div class="tagsBox">
            <p>分类：</p>
            <el-tag
              v-for="item in tags"
              size="medium"
              :effect="item.tagId===blogType?'dark':'plain'"
              @click="filter(item.tagId)"
              :key="item.tagId"
              :type="item.name"
            >{{ item.name }}</el-tag>
          </div>
          <div class="wordsBox"></div>
        </div>
      </div>
    </template>
  </div>
</template>

<script>
import blogInfo from "./blogInfo";
export default {
  data() {
    return {
      blogList: [],
      showType: 0, //0博客列表，1博客详情
      blogObj: {},
      blogType: 0, //博客分类，0全部
      tags: [],
      pageIndex: 1, //当前页
      pageSize: 10, //每页展示数
      total: 5 //总条数
    };
  },
  components: {
    blogInfo
  },
  computed: {
    filterBlog() {
      if (this.blogType == 0) {
        return this.blogList;
      } else {
        return this.blogList.filter(item => {
          let tags = item.tags.find(tag => {
            return tag.tagId == this.blogType;
          });
          if (tags) {
            return item;
          }
        });
      }
    },
    currentIndex() {
      this.total = this.filterBlog.length; //设置总条数
      // 计算当前页的数据
      return this.filterBlog.filter((item, index) => {
        return Math.floor(index / this.pageSize) === this.pageIndex - 1;
      });
    }
  },
  mounted() {
    this.load();
  },
  methods: {
    load() {
      this.$axios.get("/api/blog").then(res => {
        this.blogList = res.data;
      });
      this.$axios.get("/api/tag").then(res => {
        this.tags = res.data;
        this.tags.unshift({ tagId: 0, name: "全部" });
      });
    },
    showBlog(item) {
      this.blogObj = item;
      this.showType = 1;
    },
    reload() {
      this.blogObj = {};
      this.showType = 0;
    },
    filter(id) {
      this.blogType = id;
      this.pageIndex=1;
    }
  }
};
</script>

<style lang="scss" scoped>
.blogConBox {
  display: flex;
  flex-wrap: nowrap;
  justify-content: space-around;
}
.blogCons {
  width: 40%;
  .marBox {
    margin: 20px 0px;
    padding: 5px;
    background-color: white;
    border-radius: 5px;
  }
  .list-enter-active,
  .list-leave-active {
    transition: all 0.6s;
  }
  .list-enter, .list-leave-to
/* .list-leave-active for below version 2.1.8 */ {
    opacity: 0;
    transform: translateY(30px);
  }
  .list-move {
    transition: transform 0.6s;
  }
}
.blogFilter {
  width: 400px;

  .blogSticky {
    position: sticky;
    top: 70px;
    z-index: 9;
    padding: 5px;
    .tagsBox {
      padding: 20px;
      background-color: white;
      border-radius: 5px;
      .el-tag {
        margin-right: 10px;
        margin-top: 10px;
        cursor: pointer;
      }
    }
    .wordsBox {
      margin-top: 30px;
      padding: 20px;
      background-color: white;
      border-radius: 5px;
    }
  }
}
.blogBox {
  padding-top: 40px;
  min-height: 140px;
  cursor: pointer;
  border-radius: 5px;
  background-color: white;
  box-shadow: 2px 2px 2px 2px rgba(0, 0, 0, 0.02), 0 0 2px rgba(0, 0, 0, 0.2);
  margin-bottom: 20px;

  .blogTit {
    font-size: 28px;
    font-weight: bolder;
    padding: 0px 30px;
    margin-bottom: 10px;
  }
  .blogContent {
    font-size: 16px;
    font-weight: bold;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    padding: 0px 30px;
    margin-bottom: 10px;
    .el-tag {
      margin-right: 10px;
    }
  }
  .blogTime {
    background-color: #e9ecef;
    padding: 0px 30px;
    line-height: 300%;
    color: #707070;
    display: flex;
    justify-content: space-between;
    i {
      line-height: 300%;
    }
  }
}

.blogRightBox {
  position: sticky;
  top: 80px;
  z-index: 40;
}
.blogTagsBox {
  box-shadow: 1px 1px 1px 1px rgba(0, 0, 0, 0.02), 0 0 1px rgba(0, 0, 0, 0.15);
  border-radius: 5px;
  .blogTagsTit {
    font-weight: bold;
    font-size: 18px;
    text-align: center;
    background-color: #e9ecef;
  }
  .blogTags {
    background-color: white;
    padding: 30px 30px 20px 30px;
    border-radius: 0px 0px 5px 5px;
    span {
      margin-bottom: 10px;
      cursor: pointer;
      border: 0.5px solid #1890ff;
    }
  }
}
.blogTimeBox {
  box-shadow: 1px 1px 1px 1px rgba(0, 0, 0, 0.02), 0 0 1px rgba(0, 0, 0, 0.15);
  border-radius: 5px;
  margin-top: 60px;
  .blogTimeTit {
    font-weight: bold;
    font-size: 18px;
    text-align: center;
    background-color: #e9ecef;
  }
  .blogTimes {
    background-color: white;
    padding: 30px 30px 20px 30px;
    border-radius: 0px 0px 5px 5px;
  }
}
.flexRig {
  display: flex;
  justify-content: flex-end;
}
</style>